package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.hotLine.BindHotLineBo;
import com.zhuss.bean.bo.product.hotLineSite.CreateHotLineSiteBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.HotLineSiteDao;
import com.zhuss.product.db.entity.HotLineSite;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author mkh
 * @date 2021/1/11 13:38
 */
@Component("hotLineBind")
public class HotLineBind extends AbstractProcessor<Object, BindHotLineBo> implements Processor<Object, BindHotLineBo> {

    @Autowired
    private HotLineSiteDao hotLineSiteDao;

    @Override
    public void check(BindHotLineBo bo) throws DzcxException {
        // TODO 创建 线路 校验， 请补充
        if (bo.getHotLineId() == null) {
            throw new DzcxException(ExceptionCode.线路ID为空);
        }
    }

    @Override
    public Object process(BindHotLineBo bo) throws DzcxException {
        //根据线路id删除已绑定的站点。
        HotLineSite lineSite = new HotLineSite();
        lineSite.setHotLineId(bo.getHotLineId());
        lineSite.setDeleted(bo.getDeleted());
        lineSite.setModifierId(bo.getConcurrentUserId());
        lineSite.setModifier(bo.getConcurrentUserName());
        lineSite.setModifyTime(new Date());
        hotLineSiteDao.deleteByHotLineId(lineSite);
        //插入t_line_site表
        for (CreateHotLineSiteBo siteBo :bo.getSiteList()) {
            lineSiteSave(siteBo, bo.getHotLineId());
        }
        return null;
    }

    public void lineSiteSave(CreateHotLineSiteBo param, Long lineId) {
        HotLineSite record = new HotLineSite();
        BeanUtils.copyProperties(param, record);
        record.setHotLineId(lineId);
        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        hotLineSiteDao.create(record);
    }

    @Override
    public Object log(BindHotLineBo param) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.热门线路创建.key());
        record.setTargetId(param.getHotLineId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
